﻿using Newtonsoft.Json;
using PermissionManager.Attributes;
using PermissionManager.BLL;
using PermissionManager.Model;
using PermissionManager.Models;
using PermissionsManager.BLL;
using PermissionsManager.Core;
using PermissionsManager.Models;
using System;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.WebPages.Html;

namespace PermissionsManager.Controllers
{
    [AllowAnonymous]
    public class LoginController : Controller
    {
        private AdminUserService adminUserService = new AdminUserService();

        // GET: Login
        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>        
        public ActionResult Login()
        {
            if (AdminUserContext.context.UserInfo_no != null)
            {
                return RedirectToAction("Index", "Home");
            }
            //再次浏览器判断是否有值
            //var cookie = System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
            var name = this.User.Identity.IsAuthenticated ? this.User.Identity.Name : null;
            if (name != null)
            {
                //获取存在的cookie
                //var ticket = FormsAuthentication.Decrypt(cookie.Value);
                //string userData = ticket.UserData;
                if (!string.IsNullOrEmpty(name))
                {
                    //存储在session里面
                    AdminUser adminInfo = new AdminUser();
                    adminInfo.Id = Convert.ToInt32(name.Split('&')[1]);
                    adminInfo.Name = name.Split('&')[0];
                    AdminUserContext.context.UserInfo_no = adminInfo;
                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }
        //登录

        public JsonResult GetAdminUser(AdminUser adminUser, bool opr)
        {

            Opretation opretation = new Opretation();
            //AdminUser user = adminUserService.ValidateLogin(adminUser);
            AdminUser user = adminUserService.Query(a=>a.Name==adminUser.Name&&a.Password==adminUser.Password);
            opretation.Success = user != null;
            if (opretation.Success)
            {
                AdminUserContext.context.UserInfo_no = user;
                if (opr)
                {
                    //判断是否记住密码
                    string userData = user.Name + "&" + user.Id;
                    ////数据放入ticket
                    //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, adminUser.Name, DateTime.Now, DateTime.Now.AddMinutes(60), false, userData);
                    ////数据加密
                    //string enyTicket = FormsAuthentication.Encrypt(ticket);
                    //HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
                    //System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                    FormsAuthentication.SetAuthCookie(userData, opretation.Success);
                }
            }
            return Json(opretation);
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public ActionResult OutLogin()
        {
            AdminUserContext.context.UserInfo_no = null;
            FormsAuthentication.SignOut();
            return RedirectToAction("Login");
        }
    }
}